﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using GCTrackableRace.Interfaces;
//using System.IO;
//using System.Reflection;
//using System.Diagnostics;

//namespace GcTrackableRace.Persistence.RaceRules
//{
//    public class RaceRules
//    {
//        public List<IRaceRule> ReadFromCsvFile(string rulesFileLocation)
//        {
//            var lines = ReadLinesFromCsvFile(rulesFileLocation);

//            var nameAndParamList = CreateObjectStringsCsvValues(lines);

//            List<IRaceRule> raceRules = CreateRaceRulesFromStringLists(nameAndParamList);

//            return raceRules;
//        }

//        private List<IRaceRule> CreateRaceRulesFromStringLists(List<Tuple<string, List<string>>> nameAndParamList)
//        {
//            var rules = new List<IRaceRule>();

//            foreach(var nameAndParam in nameAndParamList)
//            {
//                var className = nameAndParam.Item1;
//                var classParams = new object[nameAndParam.Item2.Count];

//                Type objType = Assembly.Load("GcTrackableRace.ScoreCalculator").GetTypes().First(t => t.Name == className);

//                var ctors = objType.GetConstructors();
//                var ctor = ctors[0];

//                var counter = 0;
//                foreach (var param in ctor.GetParameters())
//                {
//                    Debug.WriteLine(string.Format(
//                        "Param {0} is named {1} and is of type {2}",
//                        param.Position, param.Name, param.ParameterType));

//                    Type theType = Type.GetType(param.ParameterType.ToString());

//                    if (theType == typeof(string))
//                    {
//                        classParams[counter] = nameAndParam.Item2[counter];
//                    }
//                    if (theType == typeof(int))
//                    {
//                        classParams[counter] = int.Parse(nameAndParam.Item2[counter]);
//                    }
//                    if (theType == typeof(DateTime))
//                    {
//                        classParams[counter] = DateTime.Parse(nameAndParam.Item2[counter]);
//                    }
//                    //if (theType == typeof(Decimal))
//                    //{
//                    //    classParams[counter] = Decimal.Parse(nameAndParam.Item2[counter]);
//                    //}

//                    counter++;
//                }

//                var rule = Activator.CreateInstance(objType, classParams) as IRaceRule;

//                rules.Add(rule);
//            }

//            return rules;
//        }

//        private List<Tuple<string,List<string>>> CreateObjectStringsCsvValues(IEnumerable<string> lines)
//        {
//            // TODO: Fix the naming and way this works. It looks horible!

//            var tupleList = new List<Tuple<string, List<string>>>();

//            foreach (var line in lines)
//            {
//                var lineValues = line.Split(',');

//                var name = string.Empty;
				
//                var parameters = new List<string>();

//                for (int i = 0; i < lineValues.Count(); i++)
//                {
//                    if (i == 0)
//                    {
//                        name = lineValues[i].Trim('\'', ' ');
//                    }
//                    else
//                    {
//                        parameters.Add(lineValues[i].Trim('\'',' '));
//                    }
//                }

//                tupleList.Add(new Tuple<string, List<string>>(name, parameters));
//            }

//            return tupleList;
//        }

//        private List<String> ReadLinesFromCsvFile(string rulesFileLocation)
//        {
//            return File.ReadAllLines(rulesFileLocation).ToList();
//        }
//    }
//}